define(function (require, exports, module) {
    require("apps/ebom/page.css");
    require("apps/grid");

    exports.init = function (ctx, uuid, config) {
        var page = new $.PageDomFactory(uuid);
        exports.pageFactory = page;

        var template = '<div class="choose-item" data-item="{{id}}">' +
            '<span class="title">用户名/姓名：</span>' +
            '{{userNumber}}/{{userName}}</div>';


        function checkItem(row) {
            var box = page.getJqDom("chooseItem");
            if(box.children().length == 1){
                box.children().hide();
            }
            var item = box.find("[data-item=" + row.id + "]");
            if (item.length) {
                return;
            }
            require("plugins/jquery.mustache");
            $(Mustache.render(template, row)).appendTo(box).data("chooseItem", row);
        }

        function unCheckItem(row) {
            var box = page.getJqDom("chooseItem");
            var item = box.find("[data-item=" + row.id + "]");
            if (!item.length) {
                return;
            }
            item.remove();
            if(box.children().length == 1){
                box.children().show();
            }
        }

        exports.getChooseData = function(){
            var results = [];
            page.getJqDom("chooseItem").children(".choose-item").each(function(){
                var data = $(this).data("chooseItem");
                results.push(data);
            });
            return results;
        };

        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "grid",
                myToolbar: "news_tb",
                colbar: "user_list_btn"
            },
            configs: {
                gridUrl: ctx + "/app/api/sysMember/conditionList?available=1",
                idField: "id",
                treeField: "eName",
                gridColumns: [
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {
                        field: 'temp1',
                        hidden: true
                    },
                    {
                        field: 'userNumber',
                        title: '用户名',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'userName',
                        title: '姓名',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'userHead',
                        title: '头像',
                        width: 50,
                        align: 'left',
                        formatter: function (value, row, index) {
                            var v = ctx + "/print/upload/default/pic1.png";
                            if(value){
                                v = ctx + "/print/" + value;
                            }
                            return '<img data-id="userHead" src="'+ v + '" style="width: 36px;height: 36px;" >';
                        }
                    },
                    {
                        field: 'orgsDescription',
                        title: '小组',
                        width: 100,
                        align: 'left',
                        formatter: function (value, row, index) {
                            return value || "<a>N/A</a>";
                        }
                    }
//                    {
//                        field: 'email',
//                        title: '邮箱',
//                        width: 50,
//                        align: 'left',
//                        formatter: function (value, row, index) {
//                            return !value ? "<a>N/A</a>" : value;
//                        }
//                    },
//                    {
//                        field: 'mobile',
//                        title: '手机',
//                        width: 50,
//                        align: 'left',
//                        formatter: function (value, row, index) {
//                            return !value ? "<a>N/A</a>" : value;
//                        }
//                    },
//                    {
//                        field: 'available',
//                        title: '状态',
//                        width: 50,
//                        align: 'left',
//                        formatter: function (value, row, index) {
//                            if (value == "1"
//                                && value != null) {
//                                return "启用";
//                            } else {
//                                return "禁用";
//                            }
//                        }
//                    }
                ],
                onLoadSuccess: $.noop,
                onCheck: function (index, row) {
                    if(!row.bomGroup){
                        checkItem(row);
                    }
                },
                onUncheck: function (index, row) {
                    if(!row.bomGroup){
                        unCheckItem(row);
                    }
                },
                onCheckAll:function(rows){
                    for(var i=0;i<rows.length;i++){
                        if(!rows[i].bomGroup){
                            checkItem(rows[i]);
                        }
                    }
                },
                onUncheckAll:function(rows){
                    for(var i=0;i<rows.length;i++){
                        if(!rows[i].bomGroup){
                            unCheckItem(rows[i]);
                        }
                    }
                }
            },
            events: {
                renderColBarBefore: function (key, row) {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "view":
                            whenView(context, row);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "search_all":
                            context.searchAll();
                            break;
                        case "search":
                            context.search(btn.href, "用户搜索", "user_search");
                            break;
                    }
                }
            }
        });

        var globalWindow = new $.PageWindow();

        function whenView(context, row) {
            context.detail("用户详情", row);
        }
    }
});